package cn.com.ejiakanghu.dto;

import cn.com.ejiakanghu.goods.dto.GoodsPriceInfo;
import cn.com.ejiakanghu.goods.dto.GoodsTag;
import cn.com.ejiakanghu.util.ParamConfig;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.db.domain.AgedmallOnlineService;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.vo.app.GoodsVipSalesDto;
import org.linlinjava.litemall.db.vo.app.ServiceProductVo;
import org.linlinjava.litemall.db.vo.app.StoreGoodsVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author houzb
 * @Description
 * @create 2021-04-14 21:21
 */
public class GoodsBaseData {
    /**
     * 门店商品id
     */
    private int id;
    /**
     * 商品名称
     */
    private String name;
    /**
     * 商品主图URL，图片宽高尺寸比例:320*320
     */
    private String mainImage;
    /**
     * 商品类型
     */
    private String type;

    /**
     * 商品标签列表
     */
    private List<GoodsTag> tags = new ArrayList<>();

    /**
     * 商品价格信息
     */
    private GoodsPriceInfo priceInfo;
    /**
     * 城市
     */
    private String city;
    /**
     * 省份
     */
    private String province;
    /**
     * 商品是否已下架
     */
    private int isOffline;
    /**
     * 商品是否已失效,商品可以看，但不能购买
     */
    private int isInvalid;
    /**
     * 商品是否不支持购买，商品可以看但不能买
     */
    private int isDisabled;

    /**
     * 购买或租赁当前商品可获得的抵扣金数量
     */
    private double deductNum;

    /**
     * 抵扣金状态
     */
    private Integer deductStatus;

    private String shareTitle;

    private Integer isHospital;

    private Integer isOnlyHospital;

    private Integer creditLease;

    /**
     * vip会员价格列表
     */
    private List<GoodsVipSalesDto> vipSales;

    public List<GoodsVipSalesDto> getVipSales() {
        return vipSales;
    }

    public void setVipSales(List<GoodsVipSalesDto> vipSales) {
        this.vipSales = vipSales;
    }

    public Integer getCreditLease() {
        return creditLease;
    }

    public void setCreditLease(Integer creditLease) {
        this.creditLease = creditLease;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMainImage() {
        return mainImage;
    }

    public void setMainImage(String mainImage) {
        this.mainImage = mainImage;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<GoodsTag> getTags() {
        return tags;
    }

    public void setTags(List<GoodsTag> tags) {
        this.tags = tags;
    }

    public GoodsPriceInfo getPriceInfo() {
        return priceInfo;
    }

    public void setPriceInfo(GoodsPriceInfo priceInfo) {
        this.priceInfo = priceInfo;
    }

    public int getIsOffline() {
        return isOffline;
    }

    public void setIsOffline(int isOffline) {
        this.isOffline = isOffline;
    }

    public int getIsInvalid() {
        return isInvalid;
    }

    public void setIsInvalid(int isInvalid) {
        this.isInvalid = isInvalid;
    }

    public int getIsDisabled() {
        return isDisabled;
    }

    public void setIsDisabled(int isDisabled) {
        this.isDisabled = isDisabled;
    }

    public double getDeductNum() {
        return deductNum;
    }

    public void setDeductNum(double deductNum) {
        this.deductNum = deductNum;
    }

    public Integer getDeductStatus() {
        return deductStatus;
    }

    public void setDeductStatus(Integer deductStatus) {
        this.deductStatus = deductStatus;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getShareTitle() {
        return shareTitle;
    }

    public void setShareTitle(String shareTitle) {
        this.shareTitle = shareTitle;
    }

    public Integer getIsHospital() {
        return isHospital;
    }

    public void setIsHospital(Integer isHospital) {
        this.isHospital = isHospital;
    }

    public Integer getIsOnlyHospital() {
        return isOnlyHospital;
    }

    public void setIsOnlyHospital(Integer isOnlyHospital) {
        this.isOnlyHospital = isOnlyHospital;
    }

    public static GoodsBaseData getInstance(LitemallGoods goods) {
        GoodsBaseData goodsBaseData = new GoodsBaseData();
        int goodsId = goods.getId();
        goodsBaseData.setId(goodsId);
        goodsBaseData.setName(goods.getName());
        goodsBaseData.setMainImage(goods.getPicUrl());
        goodsBaseData.setProvince(goods.getProvince());
        goodsBaseData.setCity(goods.getCity());
        goodsBaseData.setIsOffline(goods.getIsOnSale() ? 0 : 1);

        String tags = goods.getTags();
        if(StringUtils.isNotBlank(tags)) {
            goodsBaseData.setTags(JacksonUtil.gson.fromJson(tags, List.class));
        }

        goodsBaseData.setType(goods.getGoodsType());
        goodsBaseData.setPriceInfo(new GoodsPriceInfo(goods));
        return goodsBaseData;
    }

    public static GoodsBaseData getInstance(StoreGoodsVo storeGoods) {
        GoodsBaseData goodsBaseData = new GoodsBaseData();
        goodsBaseData.setId(storeGoods.getStoreGoodsId());
        goodsBaseData.setName(storeGoods.getName());
        String pic = storeGoods.getPicUrl();
        if (StringUtils.isNotEmpty(pic) && !pic.startsWith("http")) {
            pic = ParamConfig.imgBaseUrl + pic;
        }
        goodsBaseData.setMainImage(pic);
        goodsBaseData.setProvince(storeGoods.getProvince());
        goodsBaseData.setCity(storeGoods.getCity());
        String tags = storeGoods.getTags();
        if(StringUtils.isNotBlank(tags)) {
            goodsBaseData.setTags(JacksonUtil.gson.fromJson(tags, List.class));
        }
        goodsBaseData.setType(storeGoods.getGoodsType());
        goodsBaseData.setPriceInfo(new GoodsPriceInfo(storeGoods));
        goodsBaseData.setShareTitle(storeGoods.getShareUrl());
        goodsBaseData.setIsHospital(storeGoods.getIsHospital());
        goodsBaseData.setIsOnlyHospital(storeGoods.getOnlyToHospital());
        goodsBaseData.setCreditLease(storeGoods.getCreditLease());

        boolean isOnSale = storeGoods.getIsOnSale() != null ? storeGoods.getIsOnSale() : false;
        boolean deleted = storeGoods.getDeleted() != null ? storeGoods.getDeleted() : false;
        boolean storeGoodsDeleted = storeGoods.getStoreGoodsDeleted() != null ? storeGoods.getStoreGoodsDeleted() : false;
        boolean online = isOnSale && !deleted && !storeGoodsDeleted;
        goodsBaseData.setIsOffline(online ? 0 : 1);
        return goodsBaseData;
    }

    public static GoodsBaseData getInstance(AgedmallOnlineService onlineService, ServiceProductVo product) {
        GoodsBaseData goodsBaseData = new GoodsBaseData();
        goodsBaseData.setId(onlineService.getId());
        goodsBaseData.setName(onlineService.getName());
        // 服务图片取第一张
        String pics = onlineService.getServiceImage();
        if (StringUtils.isNotEmpty(pics)) {
            List<String> picList = JSONObject.parseArray(pics, String.class);
            goodsBaseData.setMainImage(CollectionUtils.isNotEmpty(picList) ? picList.get(0) : null);
        }
        goodsBaseData.setType("service");

        GoodsPriceInfo goodsPriceInfo = new GoodsPriceInfo();

        BigDecimal price = null != product.getCityProductPrice() ? product.getCityProductPrice() : product.getPrice();
        goodsPriceInfo.setPrice(null != price ? price.doubleValue() : null);
        goodsPriceInfo.setLinePrice(null == product.getOriginalPrice() ? null : product.getOriginalPrice().doubleValue());
        goodsPriceInfo.setSuffix("次");
        goodsBaseData.setPriceInfo(goodsPriceInfo);

        goodsBaseData.setIsOffline(onlineService.getStat().equals(1) ? 0 : 1);
        return goodsBaseData;
    }
}
